package com.jch.string_encoder;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

// 字符串编码器使用示例
public class DemoServer {

    public static void main(String[] args) {
        new DemoServer().bind(1116);
    }

    private void bind(int port) {
        // 接收连接线程池
        EventLoopGroup boos = new NioEventLoopGroup(1);
        // 处理连接线程池
        EventLoopGroup worker = new NioEventLoopGroup();
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boos, worker)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childHandler(new DemoChannelInitializer());

            ChannelFuture channelFuture = bootstrap.bind(port).sync();
            System.out.println("Netty Demo Server Start Done!");
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            worker.shutdownGracefully();
            boos.shutdownGracefully();
        }
    }
}
